package com.example.lotterysystem.controller;

import com.example.lotterysystem.common.util.JacksonUtil;
import com.example.lotterysystem.common.errorcode.ControllerErrorCodeConstants;
import com.example.lotterysystem.common.exception.ControllerException;
import com.example.lotterysystem.common.pojo.CommonResult;
import com.example.lotterysystem.controller.param.UserRegisterParam;
import com.example.lotterysystem.controller.result.UserRegisterResult;
import com.example.lotterysystem.service.UserService;
import com.example.lotterysystem.service.dto.UserRegisterDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    private UserService userService;
    private static final Logger logger= LoggerFactory.getLogger(UserController.class);
    @RequestMapping("/register")
    public CommonResult<UserRegisterResult> userRegister(@Validated @RequestBody UserRegisterParam param){
    logger.info("userRegister UserRegisterParam:{}", JacksonUtil.writeValueAsString(param));
        UserRegisterDTO userRegisterDTO=userService.register(param);
     return CommonResult.success(convertToUserRegisterResult(userRegisterDTO));
    }
    private UserRegisterResult convertToUserRegisterResult(UserRegisterDTO userRegisterDTO){
        UserRegisterResult userRegisterResult=new UserRegisterResult();
        if(null==userRegisterDTO){
            throw  new ControllerException(ControllerErrorCodeConstants.REGISTER_ERROR);
        }
         userRegisterResult.setUserId(userRegisterDTO.getUserId());
      return  userRegisterResult;
    }
}
